/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GroupChat;

/**
 *
 * @author TienNT
 */
public class ConfirmGroupChatRequest extends javax.swing.JFrame {

    Client.Client mClient;
    String mReceivedCommand;
    String mGroupID;
    int decide = 0;

    /**
     * mReceivedCommand struture: code:inviter:message
     */
    /**
     * Creates new form ConfirmGroupChatRequest
     */
    public ConfirmGroupChatRequest(Client.Client client, String invitation) {
        mClient = client;
        mReceivedCommand = invitation;
        initComponents();
        String[] temp = invitation.split(":");
        mGroupID = temp[1];
        txtInviter.setText(temp[2] + "  has invited you to join a group chat.");
        if (temp.length == 4) {
            txtMessage.setText(temp[3]);
        }
    }

    public int showDialog() {
        setVisible(true);
        while (true) {
            if (!isVisible()) {
                return decide;
            }
        }
    }

    public int getGroupId() {
        return Integer.parseInt(mGroupID);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JTextArea();
        txtInviter = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnAccept = new javax.swing.JButton();
        btnReject = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        txtMessage.setEditable(false);
        txtMessage.setColumns(20);
        txtMessage.setRows(5);
        jScrollPane1.setViewportView(txtMessage);

        jLabel2.setText("Would you like to join?");

        btnAccept.setText("Yes, i'd like to.");
        btnAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcceptActionPerformed(evt);
            }
        });

        btnReject.setText("No, i don't want");
        btnReject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRejectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnAccept)
                        .addGap(33, 33, 33)
                        .addComponent(btnReject)
                        .addGap(0, 41, Short.MAX_VALUE))
                    .addComponent(txtInviter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtInviter, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAccept)
                    .addComponent(btnReject))
                .addGap(5, 5, 5))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcceptActionPerformed
        // TODO add your handling code here:
        /**
         * Gui tin co ma la group toi tat ca group conversasion thong bao la
         * client do da accept
         */
        decide = 1;
        mClient.sendMessageToServer(Client.ClientUtils.GROUP_CHAT_ACCEPT + ":" + mGroupID);
        setVisible(false);
    }//GEN-LAST:event_btnAcceptActionPerformed

    private void btnRejectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRejectActionPerformed
        // TODO add your handling code here:
        mClient.sendMessageToServer(Client.ClientUtils.GROUP_CHAT_DECLINE + ":" + mGroupID);
        setVisible(false);
    }//GEN-LAST:event_btnRejectActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAccept;
    private javax.swing.JButton btnReject;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel txtInviter;
    private javax.swing.JTextArea txtMessage;
    // End of variables declaration//GEN-END:variables
}
